<?php

require 'dbconfig.php';

class User {

    function checkUserGoogle($uid, $oauth_provider, $username, $email, $first_name, $last_name)
	{
		$oauth_provider_is_facebook = 0;
		$oauth_provider_is_google = 0;
		if($oauth_provider == 'Facebook')
			$oauth_provider_is_facebook = 1;
		else
			$oauth_provider_is_google = 1;
		
        $userstable = USERS_TABLE_NAME;
        $query = mysql_query("SELECT * FROM `$userstable` WHERE email = '$email' ") or die(mysql_error());
        $result = mysql_fetch_array($query);
        if (!empty($result)) {
		
		print_r($result);
		echo $oauth_provider_is_facebook . $result['oauth_provider_is_google'].$result['oauth_provider_is_facebook'];
			if($oauth_provider_is_facebook == 1 && $result['oauth_provider_is_google']==1 && $result['oauth_provider_is_facebook']!=1)
				$updateQuery="update `$userstable` set oauth_provider_is_facebook = 1 where email='$email'";
			elseif($oauth_provider_is_google == 1 && $result['oauth_provider_is_google']!=1 && $result['oauth_provider_is_facebook']==1)
				$updateQuery="update `$userstable` set oauth_provider_is_google = 1 where email='$email'";
				
			echo $updateQuery;
			$res = mysql_query($updateQuery);

        } else {
			//	print_r( $result );
		// exit;
            #user not present. Insert a new Record
            echo "INSERT INTO `$userstable` (oauth_provider_is_google, oauth_provider_is_facebook, oauth_uid, username, email) VALUES ('$oauth_provider_is_google','$oauth_provider_is_facebook', '$uid', '$username', '$email', '$first_name', '$last_name')";
            echo "<br/>INSERT INTO `$userstable` (oauth_provider_is_google, oauth_provider_is_facebook, oauth_provider, oauth_uid, username, email) VALUES ('$oauth_provider_is_google','$oauth_provider_is_facebook', '$oauth_provider', $uid, '$username', '$email', '$first_name', '$last_name')";
            $query = mysql_query("INSERT INTO `$userstable` (oauth_provider_is_google, oauth_provider_is_facebook, oauth_provider, oauth_uid, username, email, first_name, last_name) VALUES ('$oauth_provider_is_google','$oauth_provider_is_facebook', '$oauth_provider', '$uid', '$username', '$email', '$first_name', '$last_name')") or die(mysql_error());
            $query = mysql_query("SELECT * FROM `$userstable` WHERE email = '$email' and oauth_provider = '$oauth_provider'");
            $result = mysql_fetch_array($query);
           return $result;
        }
        return $result;
    }

    

}

?>
